package com.gogowan.agroenterprise.newview;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gogowan.agroenterprise.MyApplication;
import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.base.BaseActivity;
import com.gogowan.agroenterprise.model.LogsBean;
import com.gogowan.agroenterprise.utils.ToastUtils;
import com.gogowan.agroenterprise.widget.adapter.MainLogsAdapater;
import com.google.gson.reflect.TypeToken;
import com.ioid.utils.JsonUtils;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.lang.reflect.Type;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class LogsActivity extends BaseActivity {

    private ImageView all_top_right_im;
    private PopupWindow setPop;
    private int shaixuanState=-1;
    private ListView listView;
    private SpringView springView;
    private List<LogsBean> marketRecodeList;
    private MainLogsAdapater adapater;
    private int totalPage=1;
    private int pageSize=20;
    private TextView tv_changshang,tv_jingxiaoshang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logs);
        initView();
//        initPopupWindow();
        initData();
    }
    int page=1;

    private void initData() {
        listView=(ListView)findViewById(R.id.mainlist);
        springView=(SpringView)findViewById(R.id.main_Sv);
        springView.setFooter(new DefaultHeader(this));
        springView.setHeader(new DefaultHeader(this));
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                http(page);
            }

            @Override
            public void onLoadmore() {
                page++;
                if (totalPage>=page){
                    http(page);
                }else {
                    springView.onFinishFreshAndLoad();
                    showToast("没有更多数据了");
                }

            }
        });
//        info=(LoginResultInfo)getIntent().getSerializableExtra("loginResult");
        http(page);
    }

    private void http(int page) {
        String jaString = MyApplication.mSettings.getString(MyApplication.ALL_LOGS, "");

        try {
            Type userListType = new TypeToken<List<LogsBean>>(){}.getType();
            List<LogsBean> storeListResult= (List<LogsBean>) JsonUtils.fromJSON(jaString, userListType);
            if(storeListResult.size()%pageSize==0){
                totalPage=storeListResult.size()/pageSize;
            }else{
                totalPage=storeListResult.size()/pageSize+1;
            }
            int limit=0;
            if(totalPage==page){
                limit=storeListResult.size()%pageSize;
            }else{
                limit=pageSize;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                List<LogsBean> logsBeans=storeListResult.stream().filter(logsBean -> {
                   if (shaixuanState==-1){
                       return true;
                   }else{
                       return  (shaixuanState+"").equals(logsBean.getUsertype());
                   }
                }).skip((long) (page - 1) *pageSize).limit(limit).sorted(Comparator.comparing(LogsBean::getTime,Comparator.reverseOrder())).collect(Collectors.toList());
                setlistView(logsBeans);
            }
            springView.onFinishFreshAndLoad();


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void setlistView(List<LogsBean> list){
        if (list!=null){
            if (page==1){
                marketRecodeList=list;
                adapater=new MainLogsAdapater(this,marketRecodeList);
                listView.setAdapter(adapater);

            }else {
                marketRecodeList.addAll(list);
                adapater.notifyDataSetChanged();
            }

        }
        if (marketRecodeList.size()==0){
            findViewById(R.id.notList_Tv).setVisibility(View.VISIBLE);
            springView.setVisibility(View.GONE);
            ToastUtils.show(this,"暂无数据");
        }else{
            findViewById(R.id.notList_Tv).setVisibility(View.GONE);
            springView.setVisibility(View.VISIBLE);
        }

    }
    private void initView() {
        all_top_right_im=(ImageView)findViewById(R.id.all_top_right_im);
        all_top_right_im.setOnClickListener(view -> {
            //筛选
            showPop();
        });
        TextView textView=(TextView)findViewById(R.id.all_top_title);
        textView.setText("日   志");
        findViewById(R.id.all_top_back_btn).setOnClickListener(v -> finish());
    }

    private void showPop() {
        setPop.showAsDropDown(all_top_right_im, 0, 0);
        if(shaixuanState==-1){
            tv_changshang.setTextColor(getResources().getColor(R.color.history_right));
            tv_jingxiaoshang.setTextColor(getResources().getColor(R.color.history_right));
            tv_changshang.setBackgroundResource(R.drawable.gray_17_line_10_conver_shape);
            tv_jingxiaoshang.setBackgroundResource(R.drawable.gray_17_line_10_conver_shape);
        }else  if(shaixuanState==1){
            tv_changshang.setTextColor(getResources().getColor(R.color.main_color));
            tv_jingxiaoshang.setTextColor(getResources().getColor(R.color.history_right));
            tv_changshang.setBackgroundResource(R.drawable.blue_17_line_10_conver_shape);
            tv_jingxiaoshang.setBackgroundResource(R.drawable.gray_17_line_10_conver_shape);
        }else  if(shaixuanState==0){
            tv_changshang.setTextColor(getResources().getColor(R.color.history_right));
            tv_jingxiaoshang.setTextColor(getResources().getColor(R.color.main_color));
            tv_changshang.setBackgroundResource(R.drawable.gray_17_line_10_conver_shape);
            tv_jingxiaoshang.setBackgroundResource(R.drawable.blue_17_line_10_conver_shape);
        }
    }
    /**
     * 初始化PopupWindow
     */
//    private void initPopupWindow() {
//        setPop = new PopupWindow(this);
//        LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
//        View conentView = inflater.inflate(R.layout.popup_shaixuan_dioag, null);
//        int h = this.getWindowManager().getDefaultDisplay().getHeight();
//        int w = this.getWindowManager().getDefaultDisplay().getWidth();
//        // 设置SelectPicPopupWindow的View
//        setPop.setContentView(conentView);
//        // 设置SelectPicPopupWindow弹出窗体的宽
//        setPop.setWidth(w/2-50 );
//        // 设置SelectPicPopupWindow弹出窗体的高
//        setPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        // 设置SelectPicPopupWindow弹出窗体可点击
//        setPop.setFocusable(true);
//        setPop.setOutsideTouchable(true);
//        // 刷新状态
//        setPop.update();
//        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0000000000);
//        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
//        setPop.setBackgroundDrawable(dw);
//        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
//        // 设置SelectPicPopupWindow弹出窗体动画效果
//        // this.setAnimationStyle(R.style.AnimationPreview);
//
//        tv_changshang = (TextView) conentView.findViewById(R.id.tv_changshang);
//        tv_jingxiaoshang = (TextView) conentView.findViewById(R.id.tv_jingxiaoshang);
//
//        tv_changshang.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                setPop.dismiss();
//                if(shaixuanState==1){
//                    shaixuanState=-1;
//                }else{
//                    shaixuanState=1;
//                }
//                http(1);
//
//            }
//        });
//        tv_jingxiaoshang.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                setPop.dismiss();
//                if(shaixuanState==0){
//                    shaixuanState=-1;
//                }else{
//                    shaixuanState=0;
//                }
//                http(1);
//            }
//        });
//    }
}